package com.bnz.manager.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


import com.bnz.manager.client.ManagerClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bnz.manager.entity.BrandEntity;
import com.bnz.manager.service.BrandService;
import com.bnz.common.PageUtils;
import com.bnz.common.entity.R;



/**
 * 品牌
 *
 * @author wangye
 * @email 1149688570@qq.com
 * @date 2023-03-23 19:22:25
 */
@RestController
@RequestMapping("/mapper/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private ManagerClient managerClient;
    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("mapper:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("mapper:brand:info")
    public R info(@PathVariable("id") String id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("mapper:brand:save")
    public R save(@RequestBody BrandEntity brand){
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    //@RequiresPermissions("mapper:brand:update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("mapper:brand:delete")
    public R delete(@RequestBody String[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    // 查询所有品牌
    @GetMapping("/findAll")
    public R findAll(){
        // 将name换成text
        List<Map> collect = brandService.list().stream().map(m -> {
            Map map = new HashMap();
            map.put("id", m.getId());
            map.put("text", m.getName());
            return map;
        }).collect(Collectors.toList());
        return R.ok().put("list",collect);
    }
    // 通过微服务调用
    @GetMapping("/user/list")
    @ResponseBody
    public R list(){
        return managerClient.list();
    }
}
